"""
@author: niweinan
@file: 12-04综合联系.py
@time: 2023/12/4 15:59
"""

import pandas
import os
import json

with open("userinfo.txt", "r+") as fd:
    userinfo = json.loads(fd.read())

def save(userinfo):
    with open("userinfo.txt", "w+") as fd:
        userinfo_str = json.dumps(userinfo)
        fd.write(userinfo_str)
def login(name,passwd,balance):
    userinfo_1 ={}
    userinfo_1['passwd']=passwd
    userinfo_1['balance']=balance
    userinfo[name]=userinfo_1
    save(userinfo)

def remit(name,name_to,num):
    if name_to in userinfo:
        if userinfo[name]['balance'] > num:
            userinfo[name]['balance']-=num
            userinfo[name_to]['balance']+=num
            print(f"{name}向{name_to}汇款{num}成功")
            save(userinfo)
        else:
            print(f"{name}余额不足，无法汇款。")
    else:
        print(f"{name_to}不存在，无法转款")

while True:
    print("####ATM存取款一体机####")
    choice = input("1.登录\n2.注册\n3.退出\n请输入选择：")
    if choice == "1":
        name = input("请输入用户名：")
        passwd = input("请输入密码：")
        if name in userinfo and passwd == userinfo[name]['passwd']:
            while True:
                print(f"用户 {name} 余额：{userinfo[name]['balance']}")
                choice1 = input("a.存款\nb.取款\nc.转款\nq.退出\n##请输入选项：")
                if choice1 == "a":
                    try:
                        num = int(input("输入存款金额："))
                    except:
                        print("请正确输入金额!!!")
                        continue
                    userinfo[name]['balance'] += num
                    print("存款成功，余额为",userinfo[name]['balance'])
                    save(userinfo)
                elif choice1 == "b":
                    try:
                        num = int(input("输入取款金额："))
                    except:
                        print("请正确输入金额!!!")
                        continue
                    if userinfo[name]['balance'] < num:
                        print("余额不足，余额为", userinfo[name]['balance'])
                    else:
                        userinfo[name]['balance'] -= num
                        save(userinfo)
                elif choice1 == "c":
                    name_to = input("输入收款人：")
                    try:
                        num = int(input("输入转款金额："))
                    except:
                        print("请正确输入金额!!!")
                        continue
                    remit(name,name_to,num)
                elif choice1 == "q":
                    break
        else:
            print("账号密码错误，请重新输入")
    elif choice == "2":
        while True:
            name = input("请输入注册用户名（输入q返回上一层）：")
            if name == "q":
                break
            elif name in userinfo:
                print("用户已存在")
            else:
                passwd = input("请输入注册密码：")
                try:
                    balance = int(input("输入金额："))
                except:
                    print("请正确输入金额!!!")
                    continue
                login(name,passwd,balance)
                print(f"{name}注册成功，余额为{balance}")
    elif choice == "3":
        break
    else:
        print("输入错误，请输入选项1，2，3数字")









